import glob
import os
import shutil
import time
import pandas as pd
from DrissionPage._functions.keys import Keys
from GetDataOnly import get_data_only
from login import login_ing
from page import get_page
from 判断是否有广告弹窗 import yes_or_no
from 筛选excel import excel


def get_data(port, user_path, down_path, date_start, date_end, date, usernames):
    # 清空下载文件夹
    list = []
    for root, dirs, files in os.walk(down_path):
        # 删除文件
        for file in files:
            file_path = os.path.join(root, file)
            os.remove(file_path)
    for x in range(5):
        try:
            page = get_page(port, user_path, down_path)
            page.get("https://fxg.jinritemai.com/ffa/multiShop")
            page.wait(8)
            if page.s_eles('.auxo-btn auxo-btn-primary'):
                page.quit()
                page.wait(3)
                page = get_page(port, user_path, down_path)
                page.get("https://fxg.jinritemai.com/login/common")
            if not page.s_eles(".index_roleItem__1-Hwe") and not (
                    page.s_eles('.feige_headerIcon__3CdVk') or page.s_eles('接待')):
                login_ing(page, usernames)
            if page.s_eles('text=重试'):
                print('出现重试，登录信息异常')
                page.quit()
                continue
            page.wait.ele_deleted('text=加载中', timeout=30)
            店铺eles = page.eles('@class^index_introName')
            店铺名_list = [i.text for i in 店铺eles]
            for 店铺名 in 店铺名_list:
                page.wait.ele_deleted('text=加载中', timeout=10)
                shop_name = '抖音小店-' + 店铺名
                if os.path.exists(f"C:\\抖音小额打款\\{date}\\{shop_name}.xlsx"):
                    continue
                print(shop_name)
                page(店铺名).click(by_js=True)
                page.wait(3)
                if page.eles('.styles_headerTitle__308y8'):
                    print(shop_name + '没有权限')
                    list.append(shop_name)
                    df = pd.DataFrame()
                    df.to_excel(f"C:\\抖音小额打款\\{date}\\{shop_name}.xlsx")
                    page.wait(3)
                    page.get("https://fxg.jinritemai.com/ffa/multiShop")
                    continue
                page('text=小额打款').click(by_js=True)
                page.wait(2)
                if not page.eles('text=打款记录'):
                    print(shop_name + '没有权限')
                    list.append(shop_name)
                    df = pd.DataFrame()
                    df.to_excel(f"C:\\抖音小额打款\\{date}\\{shop_name}.xlsx")
                    page.wait(3)
                    page.get("https://fxg.jinritemai.com/ffa/multiShop")
                    continue
                page('text=打款记录').click(by_js=True)
                page.wait(3)
                if page('text=导出明细').states.is_covered:
                    yes_or_no(page)
                    page.wait(1)
                # 设置查询导出时间
                page('#apply_begin_time,apply_end_time').input(date_start, clear=True)
                page.actions.type(Keys.ENTER)
                page('.auxo-picker-input auxo-picker-input-active').input(date_end, clear=True)
                page.actions.type(Keys.ENTER)
                page.wait(1)
                page('text=查 询').click()
                page.wait(2)
                if page.s_eles('text=暂无数据'):
                    print('暂无数据')
                    df = pd.DataFrame()
                    df.to_excel(f"C:\\抖音小额打款\\{date}\\{shop_name}.xlsx")
                    page.get("https://fxg.jinritemai.com/ffa/multiShop")
                    continue
                else:
                    page('导出明细').click(by_js=True)
                    print('等待小额打款明细导出')
                    page.wait.download_begin(timeout=90)
                    page.wait.downloads_done(timeout=90)
                    print('小额打款明细导出成功')
                page.wait(3)
                # 获取文件夹中所有的.xlsx文件
                xlsx_files = glob.glob(os.path.join(down_path, "*.xlsx"))
                shutil.move(xlsx_files[0], f"C:\\抖音小额打款\\{date}\\{shop_name}.xlsx")
                # 修改EXCEL
                excel(shop_name, date, f"C:\\抖音小额打款\\{date}\\{shop_name}.xlsx")
                page.wait(1)
                page.get("https://fxg.jinritemai.com/ffa/multiShop")
            page.quit()
            return list
        except Exception as e:
            print(e)
            continue
